package org.yinxue.swing.core.panel;

import javax.swing.*;
import java.awt.*;

/**
 * 扩展
 *
 * @author zengjian
 * @create 2018-03-28 11:06
 * @since 1.0.0
 */
public class JTextAreaWithJScroll extends JScrollPane {

    private JTextArea textArea = new JTextArea();

    public JTextAreaWithJScroll() {
        super(VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_AS_NEEDED);
        textArea.setLineWrap(true);
        setViewportView(textArea);
        getVerticalScrollBar().setUnitIncrement(20);
        textArea.setFont(new Font("微软雅黑",Font.PLAIN,14));
    }

    public void setText(String text){
        textArea.setText(text);
    }

    public String getText(){
        return textArea.getText();
    }

    public void append(String msg){
        textArea.append(msg+"\n");
    }

    public void appendAndRefresh(String msg){
        textArea.append(msg);
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }

    public JTextArea getTextArea(){
        return this.textArea;
    }

    /**
     * 刷新光标位置
     */
    public void refreshCaretPosition(){
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
    
}
